﻿using CaterModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaterDal
{
    public partial class DishInfoDal
    {
        public int Insert(DishInfo di)
        {
            string sql = "insert into DishInfo(DTitle,DTypeId,DPrice,DChar,DisDelete) values (@DTitle,@DTypeId,@DPrice,@DChar,@DisDelete)";
            SQLiteParameter[] ps = {
                new SQLiteParameter("@DTitle",di.DTitle),
                new SQLiteParameter("@DTypeId",di.DTypeId),
                new SQLiteParameter("@DPrice",di.DPrice),
                new SQLiteParameter("@DChar",di.DChar),
                 new SQLiteParameter("@DisDelete",di.DIsDelete)
            };
            return SqliteHelper.ExecuteNonQuery(sql, ps);
        }
        public int Delete(int id)
        {
            string sql = "update DishInfo set dIsDelete=1  where did=@id";
            SQLiteParameter p = new SQLiteParameter("@id", id);
            return SqliteHelper.ExecuteNonQuery(sql, p);
        }
        public int Update(DishInfo di)
        {
            string sql = "update  DishInfo set dtitle=@dtitle,DTypeId=@DTypeId,DPrice=@DPrice,DChar=@DChar where did=@id";
            SQLiteParameter[] ps ={
                new SQLiteParameter("@dtitle",di.DTitle),
                new SQLiteParameter("@DTypeId",di.DTypeId),
                new SQLiteParameter("@DPrice",di.DPrice),
                new SQLiteParameter("@DChar",di.DChar),
                new SQLiteParameter("@id",di.DId)
            };
            return SqliteHelper.ExecuteNonQuery(sql, ps);
        }

        public List<DishInfo> GetList(Dictionary<string,string> dic)
        {
            string sql = "select di.*,dti.dtitle DTypeTitle from DishInfo di,DishTypeInfo dti where di.dtypeid=dti.did and di.DisDelete=0 and dti.disdelete=0";

            List<SQLiteParameter> listP = new List<SQLiteParameter>();
            //接收筛选条件
            if(dic.Count>0)
            {
                foreach (var pair in dic)
                {
                    sql += " and di."+pair.Key+" like @"+pair.Key;
                    listP.Add(new SQLiteParameter("@" + pair.Key,  "%"+pair.Value+"%"));
                }
            }

            DataTable dt = SqliteHelper.GetDataTable(sql,listP.ToArray());
            List<DishInfo> list = new List<DishInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                DishInfo di = new DishInfo();
                di.DId =Convert.ToInt32(dr["did"].ToString());
                di.DTitle = dr["dtitle"].ToString();
                di.DTypeId = Convert.ToInt32(dr["DTypeId"].ToString());
                di.DTypeTitle= dr["DTypeTitle"].ToString();
                di.DPrice =Convert.ToDecimal(dr["DPrice"].ToString());
                di.DChar = dr["DChar"].ToString();

                list.Add(di); 
            }

            return list;
        }
    }
}
